//
//  BBAverageImage.m
//  BBTouch
//
//  Created by ben smith on 5/29/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//

#import "BBAverageImage.h"


@implementation BBAverageImage
// a simple (probably too simple) averaging algo
// everything kinda relies on the calling object making the right
// choices for the input variables.
// a better way (which i may do later) would be to cache the added images
// and then generate a new average whenever one was added
// that way you wouldnt ahve to supply the number of total things
// but for now, this is fine
-(void)addImage:(NSImage*)addImage number:(int)number of:(int)total
{
	//get to source bitRep fomr the image to be added
	NSBitmapImageRep * addRep = [[addImage representations] objectAtIndex:0];
	NSBitmapImageRep * bitRep;
	// this is the first image to add, might be better to check to see if we have an
	// image instead of check the number, but for now, meh
	if (number == total) {
		// then we need to make a new blank rep for our internal image
		bitRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL 
																										 pixelsWide:[addRep pixelsWide] 
																										 pixelsHigh:[addRep pixelsHigh]
																									bitsPerSample:[addRep bitsPerSample]
																								samplesPerPixel:[addRep samplesPerPixel]
																											 hasAlpha:[addRep hasAlpha] 
																											 isPlanar:[addRep isPlanar]
																								 colorSpaceName:[addRep colorSpaceName]
																										bytesPerRow:[addRep bytesPerRow]
																									 bitsPerPixel:[addRep bitsPerPixel]];	
		[self addRepresentation:[bitRep autorelease]];
	} else {
		// not the first pass, just grab our current bitrep
		bitRep = [[self representations] objectAtIndex:0];
	}

	unsigned char *srcBytes = [bitRep bitmapData];
	unsigned char *addBytes = [addRep bitmapData];
	
	int yIndex,xIndex,positionInByteArray;
	
	for (yIndex = 0; yIndex < [bitRep pixelsHigh]; yIndex++) {
		for (xIndex = 0; xIndex < [bitRep pixelsWide]; xIndex++) {
			// basically just copy the bits over, and divide them by the eventual total
			positionInByteArray = (yIndex * [bitRep bytesPerRow]) + (xIndex * [bitRep samplesPerPixel]); 
			srcBytes[positionInByteArray] += addBytes[positionInByteArray]/total;
		}
	}
}

@end
